package impls.graphics;

import impls.classes.Constants;
import impls.classes.Position;
import jade.core.AID;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * controls output on screen
 * 
 * @author thailow
 * 
 */
public class ControlCenter {
	private static final int cellSize = 3;

	private static final int maxX = Constants.MAXCOORD;

	private static final int maxY = Constants.MAXCOORD;

	private static final int delay = Constants.INTERVAL;

	private Screen f;

	private Graphics g;

	private ScreenActor[][] map;

	private ArrayList<Position> oldCars = new ArrayList<Position>();;

	private HashMap<String, Float> carfuel;

	private HashMap<String, Float> stationprice;

	private String currentCar;

	private String currentStation;

	public ControlCenter(String name) {
		f = new Screen("Cruising Agents", this, delay, maxX * cellSize, maxY
				* cellSize);
		g = f.GetGraphics();
		ScreenActor.setStaticVars(this, g, cellSize);
		map = new ScreenActor[maxX][maxY];
		f.StartAnimations();
		carfuel = new HashMap<String, Float>();
		stationprice = new HashMap<String, Float>();
	}

	// Creates map of points and centroids
	public void buildMap(HashMap<String, Position> cars,
			HashMap<String, Position> stations) {
		Color normal = new Color(Constants.CARCOLOR);
		Color selected = new Color(Constants.SELECTEDCARCOLOR);
		// removes actors from last buildMap
		for (Position pos : oldCars) {
			ScreenActor p = new ScreenActor(normal);
			p.hide(pos.getX(), pos.getY());
		}
		oldCars = new ArrayList<Position>();
		// draws new cars
		for (Iterator<String> ci = cars.keySet().iterator(); ci.hasNext();) {
			String car = ci.next();
			Position pos = cars.get(car);
			oldCars.add(pos);
			ScreenActor p;
			if (car.equals(currentCar)) {
				p = new ScreenActor(selected);
			} else {
				p = new ScreenActor(normal);
			}
			int x = pos.getX();
			int y = pos.getY();
			p.show(x, y);
		}
		// draws new stations
		normal = new Color(Constants.STATIONCOLOR);
		selected = new Color(Constants.SELECTEDSTATIONCOLOR);
		for (Iterator<String> ci = stations.keySet().iterator(); ci.hasNext();) {
			String station = ci.next();
			Position pos = stations.get(station);
			ScreenActor p;
			if (station.equals(currentStation)) {
				p = new ScreenActor(selected);
			} else {
				p = new ScreenActor(normal);
			}
			int x = pos.getX();
			int y = pos.getY();
			p.show(x, y);
		}
		// Update fuel and price
		f.setFuel(this.getFuel(f.getCarAgent().getSelectedIndex()));
		f.setPrice(this.getPrice(f.getStationAgent().getSelectedIndex()));
	}

	// Redraws all content of the window, sends Show( x, y, true) to all actors.
	public void redrawAll() {
		for (int x = 0; x < maxX; x++) {
			for (int y = 0; y < maxY; y++) {
				if (get(x, y) != null)
					get(x, y).show(x, y);
			}
		}
	}

	public void end() {
		System.exit(1);
	}

	public ScreenActor get(int x, int y) {
		return map[x][y];
	}

	public void set(int x, int y, ScreenActor a) {
		map[x][y] = a;
	}

	public void scoreAt(int x, String s) {
		f.ScoreAt(x, s);
	}

	public int getMaxX() {
		return maxX;
	}

	public int getMaxY() {
		return maxY;
	}

	public void setupStationAgentChoice(HashMap<String, Float> s) {
		f.getStationAgent().removeAll();
		Iterator<String> it = (Iterator<String>) s.keySet().iterator();
		while (it.hasNext()) {
			AID a = new AID(it.next(), true);
			f.getStationAgent().add(a.getLocalName().substring(25));
		}

	}

	public void setupCarAgentChoice(HashMap<String, Float> s) {
		f.getCarAgent().removeAll();
		Iterator<String> it = (Iterator<String>) s.keySet().iterator();
		while (it.hasNext()) {
			AID a = new AID(it.next(), true);
			f.getCarAgent().add(a.getLocalName().substring(25));
		}
	}

	public void postMessage(String message) {
		// Write Message
		f.writeMessage(message);
	}

	public String getFuel(int index) {
		Iterator<String> it = (Iterator<String>) carfuel.keySet().iterator();
		int i = 0;
		String a = "";
		while (it.hasNext()) {
			a = (it.next().toString());
			if (i == index) {
				currentCar = a;
				return "Fuel: " + String.valueOf(carfuel.get(a));
			}
			i++;
		}
		return "no Value";
	}

	public String getPrice(int index) {
		Iterator<String> it = (Iterator<String>) stationprice.keySet()
				.iterator();
		int i = 0;
		String a = "";
		while (it.hasNext()) {
			a = (it.next().toString());
			if (i == index) {
				currentStation = a;
				return "Price: " + String.valueOf(stationprice.get(a));
			}
			i++;
		}
		return "no Value";
	}

	public HashMap<String, Float> getCarfuel() {
		return carfuel;
	}

	public void setCarfuel(HashMap<String, Float> carfuel) {
		this.carfuel = carfuel;
	}

	public HashMap<String, Float> getStationprice() {
		return stationprice;
	}

	public void setStationprice(HashMap<String, Float> stationprice) {
		this.stationprice = stationprice;
	}
}